package de.henku.jpaillier;

import java.util.Scanner;

public class Main {
    static int t,m;
    static int[] w=new int[105];
    static int[] v=new int[105];
    static int[][] dp=new int[105][1050];    public static void main(String[] ar) {
        Scanner cin=new Scanner(System.in);
        t=cin.nextInt();
        m=cin.nextInt();
        for(int i=1;i<=m;i++){
            w[i]=cin.nextInt();
            v[i]=cin.nextInt();
        }
        for(int i=1;i<=m;i++){
            for(int j=1;j<=t;j++){
                if(j>=w[i]){
                    dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);
                }else{
                    dp[i][j]=dp[i-1][j];
                }
            }
        }
        System.out.println(dp[m][t]);
    }
}
